package com.ossjk.oa.accounting.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.map.HashedMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.oa.accounting.entity.CwCpttypes;
import com.ossjk.oa.accounting.service.ICwCpttypesService;
import com.ossjk.oa.system.service.IDictionaryService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chair
 * @since 2020-05-14
 */
@RestController
@RequestMapping("/accounting/cwCpttypes")
public class CwCpttypesController extends BaseController {

	@Autowired
	private ICwCpttypesService iCwCpttypesService;
	@Autowired
	private IDictionaryService iDictionaryService;

	// @RequiresPermissions("0000")
	@RequestMapping("/list")
	public Object list(Page page) {

		Wrapper wrapper = Condition.create().orderBy("e.endtime", true);

		page = iCwCpttypesService.selectVoPage(page, wrapper);

		return responseSuccess(page);

	}

	@PostMapping("/insert")
	public Object insert(CwCpttypes cw_cpttypes) {
		Wrapper wrapper = Condition.create();

		if (iCwCpttypesService.insert(cw_cpttypes)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("00400000")
	@GetMapping("/chgState/{id}")
	public Object changeState(@PathVariable("id") String id) {

		CwCpttypes ctps = iCwCpttypesService.selectById(id);
		if (ctps.getEndtime() == null) {
			ctps.setEndtime(new Date());
		} else {
			ctps.setEndtime(null); // 如何设置为null
		}

		if (iCwCpttypesService.updateEndtime(ctps.getEndtime(), id)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}
	


	@GetMapping("/cptInuse/{dkey}")
	public Object findAllCwCpttypesInuse(@PathVariable("dkey") String dkey) {
		Map<String, Object> map = new HashedMap<>();

		map.put("ctps", iCwCpttypesService.selectCwCpttypesInuse());
		map.put("tpc", iDictionaryService.getDValueByKey(dkey).split(","));
		return responseSuccess(map);
	}

}
